// log.cpp
// implement of class CMlog
//
#include "mlog.h"
#include <stdio.h>
#include <stdarg.h>
#include <strings.h>
#include <string.h>
#include "mtools.h"

static char g_buf[1024] = {0};
CMLog::CMLog(const char *info)
{
    if (info)
    {
        bzero(g_buf, sizeof(g_buf));
        strncpy(g_buf, info, 1020);
        fprintf(stdout, "======== enter %s ========\n", g_buf);
    }
}

CMLog::~CMLog()
{
    if (strlen(g_buf)>0)
    {
        fprintf(stdout, "======= leave %s =========\n", g_buf);
        bzero(g_buf, sizeof(g_buf));
    }
}

void CMLog::Plog(const char *fmt, ...)
{
    va_list args;
    char buf[1024] = {0};

    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);

    fprintf(stdout, "[%s %d] %s\n", CMTools::GetCurDateTime(), CMTools::GetTick(), buf);
    //fprintf(stdout, "[%s]: %s\n", CMTools::GetCurTime(), buf);
}

void CMLog::Plog2(int level, const char *fmt, ...)
{
    va_list args;
    char buf[1024] = {0};

    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);

    if (level == LOG_NORMAL)
        fprintf(stdout, "%s\n", buf);
    else if (level = LOG_ERR)
        fprintf(stderr, "%s\n", buf);
}

